package nero.yncc.com.createart.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;


import com.alibaba.fastjson.JSON;

import org.json.JSONException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanArticleType;
import nero.yncc.com.createart.bean.UserBean;

/**
 * Created by nero on 2017/5/31.
 */

public class UserDataUtil {

    public static void saveUserData(Context context, UserBean userBean) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream os = new ObjectOutputStream(outputStream);
            os.writeObject(userBean);
            preferences.edit().putString("user", new String(Base64.encode(outputStream.toByteArray(), Base64.DEFAULT))).commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static UserBean getUserData(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);

        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(preferences.getString("user", "").getBytes(), Base64.DEFAULT));
        try {
            ObjectInputStream ois = new ObjectInputStream(bis);
            UserBean userBean = (UserBean) ois.readObject();
            return userBean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void delUserData(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        preferences.edit().clear().commit();
    }


    /**
     * 序列化对象
     *
     * @throws IOException
     */
    private static String serialize(Object obj) throws IOException {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        objectOutputStream.writeObject(obj);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        return serStr;
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private static Object deSerialization(String str) throws IOException,
            ClassNotFoundException {
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        Object obj = objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return obj;
    }

    public static void saveArticleType(HashMap<String, String> hashMap, Context context) {
        SharedPreferences sp = context.getSharedPreferences("article", 0);
        SharedPreferences.Editor edit = sp.edit();
        try {
            edit.putString("article", serialize(hashMap));
        } catch (IOException e) {
            e.printStackTrace();
        }
        edit.commit();
    }

    private static HashMap<String, String> getArticleType(Context context) {
        SharedPreferences sp = context.getSharedPreferences("article", 0);
        try {
            return (HashMap<String, String>) deSerialization(sp.getString("article", ""));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return new HashMap<>();
    }

    public static void getArticleType(final Activity activity) {
        if (Constants.hashmap_type.size() == 0) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("category_id", "0");
            //获取文章分类
            HttpUtils.get(activity, Constants.API_SARTICLE_TYPE, hashMap, null, HttpUtils.TYPE_ARRAY, new HttpUtils.HttpCallback() {
                @Override
                public void success(String msg, Object json) {
                    org.json.JSONArray jsonArray = (org.json.JSONArray) json;

                    for (int a = 0; a < jsonArray.length(); a++) {
                        try {
                            BeanArticleType beanMessage = JSON.parseObject(jsonArray.getString(a), BeanArticleType.class);
                            Constants.hashmap_type.put(beanMessage.getCat_name(), beanMessage.getCat_id());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    UserDataUtil.saveArticleType(Constants.hashmap_type, activity);

                }

                @Override
                public void fail(String failMsg) {

                }
            });
        }
    }
}
